package java44th.group.controller.admin.brand;

import java44th.group.bean.Bo.brand.CreateBo;
import java44th.group.bean.Bo.brand.ListBo;
import java44th.group.bean.admin.bo.brand.UpdateBo;
import java44th.group.bean.common.BaseStringUtils;
import java44th.group.bean.po.MarketBrand;
import java44th.group.bean.admin.vo.BaseRespVo;
import java44th.group.bean.admin.vo.brand.CreateVo;
import java44th.group.bean.admin.vo.brand.ListVo;
import java44th.group.service.admin.brand.BrandService;
import java44th.group.utils.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;

/**
 * @Aurhor: 11437
 * @DATE: 2022/10/12 10:13
 * @Description:品牌制造商
 * @Version 1.0
 */
@RestController
@RequestMapping("admin/brand")
public class BrandController {

    @Autowired
    BrandService brandService;


    //查询品牌商列表
    @RequiresPermissions(value={"admin:brand:list","*"},logical= Logical.OR)
    @GetMapping("list")
    public BaseRespVo list(ListBo listBo) {

        if (!StringUtil.isEmpty(listBo.getId())&&!StringUtils.isNumeric(listBo.getId())) {

            return BaseRespVo.falseParameter();
        }


        ListVo listVo = null;
        try {
            listVo = brandService.brandList(listBo);
        } catch (Exception e) {
            e.printStackTrace();

            return BaseRespVo.invalidParameter("参数错误");
        }


        return BaseRespVo.ok(listVo);

    }

    //新建品牌商
    @RequiresPermissions(value={"admin:brand:create","*"},logical= Logical.OR)
    @PostMapping("create")
    public BaseRespVo create(@RequestBody CreateBo createBo) {

        BigDecimal bigDecimal = new BigDecimal("0");

        if (!isNumber(createBo.getFloorPrice()) || new BigDecimal(createBo.getFloorPrice()).compareTo(bigDecimal) == -1 || BaseStringUtils.isEmpty(createBo.getFloorPrice()) || BaseStringUtils.isEmpty(createBo.getPicUrl())

        ) {
            return BaseRespVo.falseParameter();
        }

        CreateVo createVo = null;
        try {
            createVo = brandService.brandCreate(createBo);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("参数错误");
        }


        return BaseRespVo.ok(createVo);

    }

    //修改品牌商
    @RequiresPermissions(value={"admin:brand:update","*"},logical= Logical.OR)
    @PostMapping("update")
    public BaseRespVo update(@RequestBody UpdateBo updateBo) {
        BigDecimal bigDecimal = new BigDecimal("0");

        if (!isNumber(updateBo.getFloorPrice()) || new BigDecimal(updateBo.getFloorPrice()).compareTo(bigDecimal) == -1 || BaseStringUtils.isEmpty(updateBo.getDesc()) || updateBo.getFloorPrice() == null || BaseStringUtils.isEmpty(updateBo.getPicUrl())) {

            return BaseRespVo.falseParameter();
        }

        MarketBrand marketBrand = new MarketBrand(updateBo.getId(), updateBo.getName(), updateBo.getDesc(), updateBo.getPicUrl(), updateBo.getSortOrder()
                , new BigDecimal(updateBo.getFloorPrice()), updateBo.getAddTime(), updateBo.getUpdateTime(), updateBo.getDeleted());


        MarketBrand marketBrand1 = null;
        try {
            marketBrand1 = brandService.brandUpdate(marketBrand);
        } catch (Exception e) {
            e.printStackTrace();  return BaseRespVo.invalidParameter("参数错误");
        }

        return BaseRespVo.ok(marketBrand1);

    }

    //删除品牌商
    @RequiresPermissions(value={"admin:brand:delete","*"},logical= Logical.OR)
    @PostMapping("delete")
    public BaseRespVo delete(@RequestBody MarketBrand marketBrand) {


        try {
            brandService.brandDelete(marketBrand);
        } catch (Exception e) {
            e.printStackTrace();
            return BaseRespVo.invalidParameter("参数错误");
        }


        return BaseRespVo.ok(null);
    }

    public boolean isNumber(String str) {
        try {
            new BigDecimal(str);
        } catch (Exception e) {
            return false;
        }
        return true;
    }


}
